const year = document.getElementById('year') as HTMLElement

const days = document.getElementById('days') as HTMLElement
const hours = document.getElementById('hours') as HTMLElement
const minutes = document.getElementById('minutes') as HTMLElement
const seconds = document.getElementById('seconds') as HTMLElement

const countdown = document.getElementById('countdown') as HTMLDivElement
const loading = document.getElementById('loading') as HTMLDivElement

const currentYear = new Date().getFullYear()
const newYearTime = new Date(`January 01 ${currentYear + 1} 00:00:00`)

year.innerText = String(currentYear + 1)

setTimeout(() => {
  loading.remove()
  countdown.style.display = 'flex'
}, 1000)

setInterval(updateCountdown, 1000)

function updateCountdown() {
  const currentTime = new Date()
  const diff = Number(newYearTime) - Number(currentTime)

  const d = Math.floor(diff / 1000 / 3600 / 24)
  const h = Math.floor((diff / 1000 / 3600) % 24)
  const m = Math.floor((diff / 1000 / 60) % 60)
  const s = Math.floor((diff / 1000) % 60)

  days.innerHTML = d < 10 ? '0' + d : String(d)
  hours.innerHTML = h < 10 ? '0' + h : String(h)
  minutes.innerHTML = m < 10 ? '0' + m : String(m)
  seconds.innerHTML = s < 10 ? '0' + s : String(s)
}
